package com.zy.mc_module.wheel.recyclerview.divider;

import android.content.Context;
import android.graphics.Rect;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.zy.mc_module.utils.SizeUtils;

/**
 * Created on 2019/03/17
 * 商品列表 瀑布流设置间隔
 */
public class GridItemDivider extends RecyclerView.ItemDecoration {
    private Context context;

    public GridItemDivider(Context context) {
        this.context = context;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view
                .getLayoutParams();
        // 获取item在span中的下标
        int spanIndex = params.getSpanIndex();
        // 中间间隔
        if (spanIndex % 2 == 0) {
            outRect.left = SizeUtils.dp2px(context, 10f);
            outRect.right = SizeUtils.dp2px(context, 5f);
        } else {
            outRect.left = SizeUtils.dp2px(context, 5f);
            outRect.right = SizeUtils.dp2px(context, 10f);
        }
        int position = parent.getChildLayoutPosition(view);
        if (position == 0 || position == 1) {
            outRect.top = SizeUtils.dp2px(context, 10f);
        }
    }
}
